package com.mysterymall.Service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysterymall.Common.Comp.HttpStatus;
import com.mysterymall.Common.Utils.Result;
import com.mysterymall.Mapper.GoodsCategoryMapper;
import com.mysterymall.Mapper.GoodsMapper;
import com.mysterymall.Mapper.GoodsTypeMapper;
import com.mysterymall.Model.Dto.GoodsTypeDto;
import com.mysterymall.Model.Entity.Goods;
import com.mysterymall.Model.Entity.GoodsType;
import com.mysterymall.Model.Vo.GoodsTypeVo;
import com.mysterymall.Model.Vo.GoodsVo;
import com.mysterymall.Model.Vo.ShowGoodsVo;
import com.mysterymall.Service.GoodsTypeService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName com.mysterymall.Service.Impl
 * @Description
 * @Author HuangWX
 * @Time 2023/11/06  10:40
 */
@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {



    private final GoodsTypeMapper goodsTypeMapper;

    private final GoodsMapper goodsMapper;

    private final GoodsCategoryMapper goodsCategoryMapper;


    /**
     * 构造器注入
     * @param goodsMapper 商品映射器
     * @param goodsTypeMapper 商品类别映射器
     */
    public GoodsTypeServiceImpl(GoodsMapper goodsMapper, GoodsTypeMapper goodsTypeMapper, GoodsCategoryMapper goodsCategoryMapper) {
        this.goodsMapper = goodsMapper;
        this.goodsTypeMapper = goodsTypeMapper;
        this.goodsCategoryMapper = goodsCategoryMapper;
    }

    @Override
    public Result addGoodsType(GoodsTypeDto goodsTypeDto) {

        if (!StpUtil.hasPermission("goodsType:add:*")) {
            return new Result(HttpStatus.UNAUTHORIZED, "message.powerError");
        }

        // 添加按照   中  马来西亚  英语
        String[] goodsList = goodsTypeDto.getGoodsType().split("_");

        GoodsType goodsType = goodsTypeMapper.queryByName(goodsList[0]);

        if (goodsType != null) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.alikeError");
        }

        goodsType = new GoodsType();

        // 类别图片不能为空或者空字符串
        if (goodsTypeDto.getImage() == null || goodsTypeDto.getImage().length == 0) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.imageNullError");
        }

        // 填入图片
        goodsType.setImage(goodsTypeDto.getImage()[0]);

        goodsType.setCategoryId(goodsTypeDto.getCategory());

        goodsType.setTypeName(goodsTypeDto.getGoodsType());

        goodsTypeMapper.insert(goodsType);

        return Result.success("message.addSuccess");
    }

    @Override
    public Result getType() {

        if (!StpUtil.hasPermission("goodsType:select:*")) {
            return new Result(HttpStatus.UNAUTHORIZED, "message.powerError");
        }

        QueryWrapper<GoodsType> queryWrapper = new QueryWrapper<>();
        List<GoodsType> goodsTypeList = goodsTypeMapper.selectList(queryWrapper);

        List<GoodsTypeVo> goodsTypeVos = new ArrayList<>();
        int index = 1;
        for (GoodsType goodsType :
                goodsTypeList) {
            GoodsTypeVo goodsTypeVo = new GoodsTypeVo();
            goodsTypeVo.setGoodsTypeZh(goodsType.getTypeName().split("_")[0]);
            goodsTypeVo.setGoodsTypeMs(goodsType.getTypeName().split("_")[1]);
            goodsTypeVo.setGoodsTypeEn(goodsType.getTypeName().split("_")[2]);
            goodsTypeVo.setIndex(index);
            goodsTypeVo.setCategory(goodsCategoryMapper.selectById(goodsType.getCategoryId()).getName());
            goodsTypeVo.setImage(goodsType.getImage());
            goodsTypeVo.setId(goodsType.getId());
            goodsTypeVo.setCreateTime(goodsType.getCreateTime());
            index++;
            goodsTypeVos.add(goodsTypeVo);
        }
        return Result.success("message.querySuccess", goodsTypeVos);

    }

    @Override
    public Result updateType(GoodsTypeDto goodsTypeDto) {

        if (!StpUtil.hasPermission("goodsType:update:*")) {
            return new Result(HttpStatus.UNAUTHORIZED, "message.powerError");
        }

        GoodsType goodsType = goodsTypeMapper.selectById(goodsTypeDto.getId());

        goodsType.setTypeName(goodsTypeDto.getGoodsType());

        goodsType.setImage(goodsTypeDto.getImage()[0]);
        goodsType.setCategoryId(goodsTypeDto.getCategory());

        goodsTypeMapper.updateById(goodsType);

        return Result.success("message.updateSuccess");
    }

    @Override
    public Result deleteGoodsType(String id) {

        if (!StpUtil.hasPermission("goodsType:delete:*")) {
            return new Result(HttpStatus.UNAUTHORIZED, "message.powerError");
        }

        // 查询商品类别
        GoodsType goodsType = goodsTypeMapper.selectById(id);

        // 查询商品中是否有该类别
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Goods::getGoodsType,goodsType.getId());
        List<Goods> goodsList = goodsMapper.selectList(queryWrapper);

        if(goodsList.size() > 0){
            return new Result(HttpStatus.BAD_NOT_PASS,"message.deleteError-该类别下有商品不允许删除");
        }

        goodsTypeMapper.deleteById(id);

        return Result.success("message.deleteSuccess");
    }

    @Override
    public Result getShowType(HttpServletRequest request) {

        // 查询所有类别
        LambdaQueryWrapper<GoodsType> queryWrapper = new LambdaQueryWrapper<>();

//        queryWrapper.select(GoodsType::getId,GoodsType::getTypeName,GoodsType::getImage);

        List<GoodsType> goodsTypeList = goodsTypeMapper.selectList(queryWrapper);

        if(goodsTypeList == null){
            return new Result(HttpStatus.BAD_NOT_PASS,"message.queryError");
        }

        List<GoodsType> goodsTypeVoList = new ArrayList<>();

        for (GoodsType goodsType :
                goodsTypeList
             ) {
            switch ((String) request.getAttribute("Lang")){
                case "zh":
                    goodsType.setTypeName(goodsType.getTypeName().split("_")[0]);
                    break;
                case "ms":
                    goodsType.setTypeName(goodsType.getTypeName().split("_")[1]);
                    break;
                case "en":
                    goodsType.setTypeName(goodsType.getTypeName().split("_")[2]);
                    break;
            }

            // 图片处理
            goodsType.setImage("/images/"+goodsType.getImage());



            goodsTypeVoList.add(goodsType);
        }

        return Result.success("message.querySuccess",goodsTypeVoList);
    }


}
